package com.jn.fastjson.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jn.fastjson.R;

import java.util.ArrayList;
import java.util.List;

public class HotSearchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_search);

        String hotSearchJsonString = getIntent().getStringExtra("hotsearch_json");
        JSONObject hotSearchJson = JSON.parseObject(hotSearchJsonString);
        JSONArray result = hotSearchJson.getJSONArray("result");

        List<String> hotSearchItems = new ArrayList<>();
        final List<String> links = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            JSONObject item = result.getJSONObject(i);
            hotSearchItems.add(item.getString("title"));
            links.add(item.getString("link"));
        }

        ListView hotSearchListView = findViewById(R.id.hot_search_list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, hotSearchItems);
        hotSearchListView.setAdapter(adapter);

        hotSearchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = links.get(position);
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }
        });
    }
}
